/* 
 * Break beam sensor. Uses two LEDs, one in reverse to work as a light
 * gathering diode.
 *
 * $AUTHOR$  Reuben Smith (smithre5)
 * $COURSE$  ECE 387, Spring 2013
 * $TEACHER$ Dr. Peter Jamieson
 *
 */
 
/* TODO
 * 	Nothing. Doesn't work for our purposes.
 */

const int SERIAL_RATE				= 9600;
// light-emitting diode
const int LED_PIN_POS				= 13;
// light-gathering diode
const int LGD_PIN_POS				= 8;
const int LGD_PIN_NEG				= 13;
const int LGD_CHARGE_DELAY	= 585;  // us, found by hand testing


void setup()
{
  pinMode(LED_PIN_POS, OUTPUT);
  digitalWrite(LED_PIN_POS, HIGH);
  
  pinMode(LGD_PIN_NEG, INPUT);
  digitalWrite(LGD_PIN_NEG, HIGH);
  
  Serial.begin(SERIAL_RATE);
}


/* lgd_collect
 * -> lgd_pin   positive pin of LGD 
 * -> time      time in us to collect charge for
 * <- 1 iff sufficient charge was collected
 *
 * Allows a charge to build up in LGD (an LED in reverse) for the given time.
 * Returns 1 if a charge was built up, 0 otherwise. Low-light conditions will
 * require a longer charge time. This won't really work with a time of 0.
 */
int lgd_collect(int lgd_pin, int time)
{
    pinMode(lgd_pin, OUTPUT);    
    digitalWrite(lgd_pin, LOW);
    pinMode(lgd_pin, INPUT);
    delayMicroseconds(time);
    
    return digitalRead(lgd_pin);
}


void loop()
{
  Serial.println(lgd_collect(LGD_PIN_POS, LGD_CHARGE_DELAY));
}
